<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="50%"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from './menu.data';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getMenuList, postMenu, putMenu } from '/@/api/sys/system';
  import { listToTree } from '/@/utils/helper/treeHelper';
  export default defineComponent({
    name: 'MenuDrawer',
    components: { BasicDrawer, BasicForm },
    emits: ['success', 'register'],
    setup() {
      const isUpdate = ref(true);
      const rowId = ref('');
      const { createMessage } = useMessage();
      const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
        labelWidth: 100,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: { lg: 12, md: 24 },
      });

      const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
        resetFields();
        setDrawerProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;

        if (unref(isUpdate)) {
          rowId.value = data.record.id;
          console.log(data.record);
          if (
            data.record.actions != null &&
            Object.prototype.toString.call(data.record.actions) !== '[object Array]'
          ) {
            data.record.actions = data.record.actions.split(',');
          }
          setFieldsValue({
            ...data.record,
          });
        }
        // //const parentTree = listToTree(await getMenuList(), { id: 'id', pid: 'parentId' });
        // const parentTree = listToTree(await getDeptList(),{ id: 'id', pid: 'parentId' });
        // //const parentTree = handleTree(await getMenuList(), 'id', 'parentId', 'children');
        // console.log(parentTree);
        // updateSchema({
        //   field: 'parentId',
        //   componentProps: { parentTree },
        // });

        const treeData = listToTree(await getMenuList(), { id: 'id', pid: 'parentId' });
        updateSchema({
          field: 'parentId',
          componentProps: { treeData },
        });
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增菜单' : '编辑菜单'));

      async function handleSubmit() {
        try {
          const form_data = await validate();
          setDrawerProps({ confirmLoading: true });
          // TODO custom api
          form_data.actions = form_data.actions.join(',');
          if (isUpdate.value) {
            await putMenu(rowId.value, form_data).then(() => {
              createMessage.success('修改成功!');
            });
          } else {
            await postMenu(form_data).then(() => {
              createMessage.success('创建成功!');
            });
          }
          closeDrawer();
        } finally {
          setDrawerProps({ confirmLoading: false });
        }
      }

      return { registerDrawer, registerForm, getTitle, handleSubmit };
    },
  });
</script>
